package cn.kgc.javaweb.dao.impl;

import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/9/8
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User selectByUsername(String username) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql ="select *  from  user where username = ?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
            return  user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void register(String username, String password){

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into user (username,password)values(?,?)";

        try {
            int update = queryRunner.update(sql, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }


    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from user where username = ? and password = ?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username, password);
            return  user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }

    @Override
    public List<User> selectAll() {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql = "select *  from user ";

        try {
            List<User> users = queryRunner.query(sql, new BeanListHandler<User>(User.class));
            return users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }

    @Override
    public Integer add(User user) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into user(username,password,tel,birth)values(?,?,?,?) ";

        //转换日期类型为string  Date  ->  String
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String birth = simpleDateFormat.format(user.getBirth());

        try {
            int line = queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getTel(), birth);
            return  line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }

    @Override
    public Integer delete(int id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from user where id = ?";
        try {
            int line = queryRunner.update(sql, id);
            return  line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }

    @Override
    public Integer updateUser(User user) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update user set password= ? , tel = ? ,birth = ? where id = ?";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String bir = simpleDateFormat.format(user.getBirth());

        try {
            int line = queryRunner.update(sql, user.getPassword(), user.getTel(), bir, user.getId());
            return  line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new  RuntimeException();
        }

    }

    @Override
    public List<User> selectByPage(Integer start, Integer size) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = " select *  from user limit ?,?  ";
        try {
            List<User> users = queryRunner.query(sql, new BeanListHandler<User>(User.class), start, size);
            return  users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }

    @Override
    public Integer updateImg(Integer userId, String lastName) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update user set img = ? where  id = ?";
        try {
            int update = queryRunner.update(sql, lastName, userId);
            return  update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new RuntimeException();
        }
    }
}
